package com.examination.controller.checkModel;

import com.examination.entity.Exam;
import com.examination.entity.Examination;
import com.examination.entity.Questions;
import com.examination.service.checkModel.ExamRecordService;
import com.examination.service.checkModel.ExamService;
import com.examination.service.checkModel.ExaminationService;
import com.examination.service.checkModel.QuestionsService;
import com.examination.util.JsonResult;
import com.examination.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(description = "考试接口")
@RestController
@RequestMapping("exam")
@CrossOrigin
public class ExamController {
    @Autowired
    private ExamService examService;

    @Autowired
    private QuestionsService questionsService;

    @Autowired
    private ExaminationService examinationService;

    @Autowired
    private ExamRecordService examRecordService;

    @ApiOperation(value = "增加考试" , notes = "测试数据:{\"exam_name\":\"基础考试\",\"specialty_id\":1,\"examination_id\":3,\"worktype_id\":2,\"exam_time\":45,\"start_time\":\"2020-04-01\",\"end_time\":\"2020-05-01\"}")
    @PostMapping("/InsertExam")
    public boolean InsertExam(@RequestBody Map map) throws ParseException {
        //`exam_name`, `section_id`, `specialty_id`, `examination_id`, `worktype_id`, `exam_time`,
        // `start_time`, `end_time`, `up_time` ,`start_timeC` ,`end_timeC`
        String start_time = (String) map.get("start_time");
        String end_time = (String) map.get("end_time");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        Date date = format.parse(start_time);
        Date date1 = format.parse(end_time);
        //日期转时间戳（毫秒）
        long times = date.getTime();
        long times1 = date1.getTime();
        map.put("start_timeC" , times);
        map.put("end_timeC" , times1);
        return examService.InsertExam(map) == 1;
    }

    @ApiOperation(value = "删除考试" , notes = "测试数据:")
    @PostMapping("/DeleteExam")
    public boolean DeleteExam(@RequestBody Map map) {
        return examService.DeleteExam(map) == 1;
    }

    @ApiOperation(value = "修改考试" , notes = "测试数据:")
    @PostMapping("/UpdateExam")
    public boolean UpdateExam(@RequestBody Map map) {
        return examService.UpdateExam(map) == 1;
    }

    @ApiOperation(value = "分页查询考试" , notes = "传参:pageNo,pageSize,user_id")
    @PostMapping("/FindExam")
    public Page<Exam> FindExam(@RequestBody Map map) {
        Page<Exam> page = new Page<Exam>();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(examService.Total(map));
        page.setItems(examService.FindExam(map));
        return page;
    }

    @ApiOperation(value = "查询最新考试App" , notes = "传参:user_id")
    @PostMapping("/FindExamNew")
    public JsonResult FindExamNew(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        //System.out.println(map);
        jsonResult.setData(examService.FindExamNew(map));
        return jsonResult;
    }

    @ApiOperation(value ="提交试卷",notes = "传参:{\"questionsid\":\"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20\",\n" +
            "\"answer\":\"true,true,false,false,true,[2014/12/1],[安全第一/预防为主/综合治理],[公司/项目/班组],[不伤害自己/不伤害他人/不被他人伤害],[干粉/水],C,B,C,B,B,C,B,C,A,C\",\n" +
            "\"type\":\"判断题,判断题,判断题,判断题,判断题,填空题,填空题,填空题,填空题,填空题,选择题,选择题,选择题,选择题,选择题,选择题,选择题,选择题,选择题,选择题\",\n" +
            "\"examination_id\":1,\"exam_id\":9,\"user_id\":1,\"staff_id\":491}")
    @PostMapping("/PushExam")
    public JsonResult PushExam(@RequestBody Map map) throws ParseException {
        System.out.println("map:"+map);
        JsonResult jsonResult = new JsonResult();
        int scoreP = 0;
        int scoreX = 0;
        int scoreT = 0;
        int scoreJ = 0;
        List list = new ArrayList(map.values());//map转list

        //题目id
        String id = (String) list.get(0);
        String[] arr=id.split(",");
        List<String> idList = Arrays.asList(arr);
        //System.out.println("idList:"+idList);

        //答案id
        String answer = (String) list.get(1);
        String[] arr1=answer.split(",");
        List<String> answerList = Arrays.asList(arr1);
        //System.out.println("answerList:"+answerList);

        //题目类型
        String type = (String) list.get(2);
        String[] arr2=type.split(",");
        List<String> typeList = Arrays.asList(arr2);
        //System.out.println("typeList:"+typeList);

        for (int i=0;i<idList.size();i++){
            map.put("id",idList.get(i));
            Questions questions = questionsService.FindQuestionsById(map);
            //System.out.println("questions:"+questions);
            //System.out.println(typeList.get(i));
            if (typeList.get(i).equals("判断题")){
                String s = String.valueOf(questions.getJudge_answer());
                if (s.equals(answerList.get(i))){
                    Examination examination = examinationService.FindExaminationById(map);
                    //System.out.println("examination:"+examination);
                    scoreP += examination.getJudge_count();
                    //System.out.println("1:"+scoreP);
                }
            }else if(typeList.get(i).equals("选择题")){
                if (questions.getSelect_answer().equals(answerList.get(i))){
                    Examination examination = examinationService.FindExaminationById(map);
                    //System.out.println("examination:"+examination);
                    scoreX += examination.getSelect_count();
                    //System.out.println("2:"+scoreX);
                }
            }else if(typeList.get(i).equals("填空题")){
                //System.out.println("answerList.get(i):"+answerList.get(i));
                String gapanswer = questions.getGap_answer();
                String ss = gapanswer.replace(",", "/");
                //System.out.println("ss:"+ss);
                if (ss.equals(answerList.get(i))){
                    Examination examination = examinationService.FindExaminationById(map);
                    //System.out.println("examination:"+examination);
                    scoreT += examination.getGap_count();
                    //System.out.println("3:"+scoreT);
                }
            }else if(typeList.get(i).equals("简答题")){
                if (questions.getShort_answer().equals(answerList.get(i))){
                    Examination examination = examinationService.FindExaminationById(map);
                    //System.out.println("examination:"+examination);
                    scoreJ += examination.getShort_count();
                    //System.out.println("4:"+scoreJ);
                }
            }
        }
        int all = scoreP+scoreX+scoreT+scoreJ;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        String time = format.format(now);
        Date date = format.parse(time);
        //日期转时间戳（毫秒）
        long times = date.getTime();
        map.put("exam_time" , time);
        map.put("exam_timeC" , times);

        if (all >= 60) {
            map.put("exam_result" , "合格");
        } else {
            map.put("exam_result" , "不合格");
        }
        map.put("exam_count",all);
        int i = examRecordService.InsertExamRecord(map);
        if (i == 1) {
            jsonResult.setCode(200);
            jsonResult.setMessage("交卷成功!");
            return jsonResult;
        } else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("交卷失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "根据user_id查询考试",notes = "传参:user_id,examination_name(试卷名称(选填))")
    @PostMapping("/FindExamAll")
    public List<Exam> FindExamAll(@RequestBody Map map){
        return examService.FindExamAll(map);
    }
}
